package day08;

/*
 * Author: JacksonCoder
 * FileName: NioFileRead
 * Version: 1.0.0
 * Date: 2025/4/15
 */

import java.io.IOException;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioFileRead {

    public static void main(String[] args) {
        try (RandomAccessFile file = new RandomAccessFile("input.txt", "r");
             FileChannel channel = file.getChannel()) {
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            while(channel.read(buffer) > 0){
                buffer.flip(); // 切换模式
                while(buffer.hasRemaining()){
                    System.out.println((char) buffer.get());
                }
                buffer.clear(); // 清空缓冲区准备下一次读取
            }
        }catch( IOException e){
            e.printStackTrace();
        }
    }

}
